package com.delos.rbac.security;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;

/**
 * 自定义认证信息类
 *
 * @author zouyaowen
 * @date 2020-05
 */
@Slf4j
@Data
public class TokenAuthentication implements Authentication {

    private Credentials credentials;
    private Long userId;
    private String userName;
    /**
     * 是否授权
     */
    private boolean authenticated;

    public TokenAuthentication() {
        super();
    }

    public TokenAuthentication(HttpServletRequest request, HttpServletResponse response) {
        this.credentials = new Credentials(request, response);
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public Object getCredentials() {
        return this.credentials;
    }

    @Override
    public Object getDetails() {
        return null;
    }

    @Override
    public Object getPrincipal() {
        return null;
    }

    @Override
    public boolean isAuthenticated() {
        return this.authenticated;
    }

    @Override
    public void setAuthenticated(boolean authenticated) throws IllegalArgumentException {
        this.authenticated = authenticated;
    }

    @Override
    public String getName() {
        return this.userName;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Credentials {
        private HttpServletRequest request;
        private HttpServletResponse response;
    }
}
